package com.yuhong.controller;

import com.yuhong.pojo.Result;
import com.yuhong.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        /*
        // 把文件存储在本地磁盘上
        String originalFileName = file.getOriginalFilename();

        // 保证文件名的唯一性
        String fileName = UUID.randomUUID().toString() +
            originalFileName.substring(originalFileName.lastIndexOf("."));

        file.transferTo(new File("D:\\upload_temp\\" + fileName));

        return Result.success("url访问地址");
        */

        //使用阿里oss
        String originalFileName = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() +
                originalFileName.substring(originalFileName.lastIndexOf("."));


        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());

        return Result.success(url);



    }
}
